using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.IO;
using System.Web;

namespace ConferenceRegister.Domain
{

    #region Validation Attributes
    public class UniqueRegistrantsValidator : ValidationAttribute
    {
        RegisterService s = new RegisterService();

        public UniqueRegistrantsValidator() 
        {  ErrorMessage = "Duplicate entry found. If registering for a family member, please provide an accurate first name.";
        }
        public override bool IsValid(object value)
        {
            Register r = value as Register;
            bool dup = s.All.Registrants.All.Where(e => e.ConferenceID == r.ConferenceID && IsSame(r.FirstName, e.FirstName) && IsSame(r.LastName, e.LastName) && r.Zip == e.Zip).Any();
            return !dup;
        }
        private bool IsSame(string a, string b)
        {
            return a.ToLower().Trim() == b.ToLower().Trim();
        }
    }
    #endregion

    [MetadataType(typeof(Register.Metadata))]
    public partial class Register
    {
        class Metadata 
        {
            [Key]
            public int ID { get; set; }
            [Required]
            public int ConferenceID { get; set; }
            [Required(ErrorMessage = " ")]
            public string FirstName { get; set; }
            [Required(ErrorMessage = " ")]
            public string LastName { get; set; }
            [Required(ErrorMessage = " ")]
            [RegularExpression(".+\\@.+\\..+", ErrorMessage = "Invalid")]
            public string Email { get; set; }
            [Required(ErrorMessage = " ")]
            public string Street { get; set; }
            public string Landmark { get; set; }
            [Required(ErrorMessage = " ")]
            public string City { get; set; }
            [Required(ErrorMessage = " ")]
            public string State { get; set; }
            [Required(ErrorMessage = " ")]
            public string Country { get; set; }
            [Required(ErrorMessage = " ")]
            [Range(10000,999999, ErrorMessage="5-6 numbers")]
            [DisplayName("Postal Code")]
            public int Zip { get; set; }
            [Required(ErrorMessage = " ")]
            //[Range(1000000000, 9999999999, ErrorMessage="10 numbers")]
            public long MobilePhone { get; set; }
            //[Range(1000000000, 9999999999, ErrorMessage = "10 numbers")]
            public Nullable<long> ResidentPhone { get; set; }
            [Required(ErrorMessage = " ")]
            public string Church { get; set; }
            [Required(ErrorMessage = " ")]
            public bool IsPastor { get; set; }
            [Required(ErrorMessage = " ")]
            public System.DateTime SubmitDate { get; set; }
            public Nullable<System.DateTime> PaymentDate { get; set; }
            public decimal PaymentAmount { get; set; }
            [Required(ErrorMessage = " ")]
            public string PaymentType { get; set; }
            [DisplayName("Cheque/DD Number")]
            public Nullable<int> PaymentNumber { get; set; }
            public bool IsLocalPickup { get; set; }
            public Nullable<System.DateTime> DeliverDate { get; set; }
            [Required(ErrorMessage = " ")]
            public int SeatID { get; set; }
        }

        public void FixPhoneNumbers()
        {
            if (!Country.ToLower().Contains("india"))
                return;
            string p = MobilePhone.ToString();
            if (!p.StartsWith("91"))
                MobilePhone = long.Parse("91"+p);
            
            if (ResidentPhone != null)
            {
                p = ResidentPhone.ToString();
                if (!p.StartsWith("91"))
                    ResidentPhone = long.Parse("91"+p);
            }
        }
    }

	[MetadataType(typeof(Conference.Metadata))]
	public partial class Conference
	{
        class Metadata { }
	}

    [MetadataType(typeof(Seat.Metadata))]
    public partial class Seat
    {
        class Metadata { }
    }

    [MetadataType(typeof(vSeat.Metadata))]
    public partial class vSeat
    {
        class Metadata { }
        public int RemainingSeats { get { return Capacity - ActualSeats; } }
    }

    [MetadataType(typeof(Ministry.Metadata))]
    public partial class Ministry
    {
        class Metadata { }
    }

    [MetadataType(typeof(vMinistry.Metadata))]
    public partial class vMinistry
    {
        class Metadata { }
    }
    [MetadataType(typeof(MinistrySignup.Metadata))]
    public partial class MinistrySignup
    {
        class Metadata { }
    }
}
